
include(ExternalProject)

if (ENABLE_NNDEPLOY_INFERENCE_MNN STREQUAL "OFF")
elseif (ENABLE_NNDEPLOY_INFERENCE_MNN STREQUAL "ON")
else()
  if(IS_ABSOLUTE ${ENABLE_NNDEPLOY_INFERENCE_MNN})
    # Absolute path, use directly
    set(ENABLE_NNDEPLOY_INFERENCE_MNN ${ENABLE_NNDEPLOY_INFERENCE_MNN})
    message(STATUS "Using absolute path for MNN: ${ENABLE_NNDEPLOY_INFERENCE_MNN}")
  else()
    # Relative path, relative to project root directory
    set(ENABLE_NNDEPLOY_INFERENCE_MNN ${CMAKE_SOURCE_DIR}/${ENABLE_NNDEPLOY_INFERENCE_MNN})
    message(STATUS "Using relative path for MNN: ${ENABLE_NNDEPLOY_INFERENCE_MNN}")
    # Update ENABLE_NNDEPLOY_INFERENCE_MNN to absolute path
    set(ENABLE_NNDEPLOY_INFERENCE_MNN ${ENABLE_NNDEPLOY_INFERENCE_MNN})
  endif()
  include_directories(${ENABLE_NNDEPLOY_INFERENCE_MNN}/include)
  set(LIB_PATH ${ENABLE_NNDEPLOY_INFERENCE_MNN}/${NNDEPLOY_THIRD_PARTY_LIBRARY_PATH_SUFFIX})
  # set(LIBS "MNN")
  file(GLOB_RECURSE LIBS ${LIB_PATH}/*${NNDEPLOY_LIB_SUFFIX}*)
  message(STATUS "MNN libs: ${LIBS}")
  foreach(FULL_LIB_NAME ${LIBS})
  # foreach(LIB ${LIBS})
    # set(LIB_NAME ${NNDEPLOY_LIB_PREFIX}${LIB}${NNDEPLOY_LIB_SUFFIX})
    # set(FULL_LIB_NAME ${LIB_PATH}/${LIB_NAME})
    if(IS_SYMLINK ${FULL_LIB_NAME})
      get_filename_component(REAL_LIB_NAME ${FULL_LIB_NAME} REALPATH)
      # message(STATUS "Real path of ${FULL_LIB_NAME}: ${REAL_LIB_NAME}")
      set(FULL_LIB_NAME ${REAL_LIB_NAME})
      # message(STATUS "Full lib name: ${FULL_LIB_NAME}")
    endif()
    set(NNDEPLOY_THIRD_PARTY_LIBRARY ${NNDEPLOY_THIRD_PARTY_LIBRARY} ${FULL_LIB_NAME})    
  endforeach()

  if(SYSTEM_Windows)
    set(BIN_PATH ${ENABLE_NNDEPLOY_INFERENCE_MNN}/bin)
    link_directories(${BIN_PATH})
    file(GLOB_RECURSE SET_BIN_PATH ${BIN_PATH}/*.dll)
    foreach(SET_BIN_PATH ${SET_BIN_PATH})
      file(COPY ${SET_BIN_PATH} DESTINATION ${EXECUTABLE_OUTPUT_PATH}/${CMAKE_BUILD_TYPE})
    endforeach()
  endif()
  install(DIRECTORY ${ENABLE_NNDEPLOY_INFERENCE_MNN} DESTINATION ${NNDEPLOY_INSTALL_THIRD_PARTY_PATH})  
endif()